<?php

namespace api\modules\v1\controllers;

use api\controllers\RestController;
use api\modules\v1\services\Classify;
use api\modules\v1\services\Course;

/**
 * Default controller for the `v1` module
 */
class ClassifyController extends RestController
{
    public $modelClass = 'api\modules\v1\services\Classify';

    /**
     * 重写create操作，进行用户注册或添加
     * @name: actions
     * @return array
     * @author: rickeryu <lhyfe1987@163.com>
     * @time: 17/11/21 下午4:57
     */
    public function actions() {
        $actions = parent::actions(); // TODO: Change the autogenerated stub
        unset($actions['create'], $actions['update'], $actions['delete'], $actions['index']);
        return $actions;
    }

    /**
     * 返回分类列表
     * @name: actionIndex
     * @return array
     * @author: rickeryu <lhyfe1987@163.com>
     * @time: 2017/11/27 下午2:34
     */
    public function actionIndex() {
        $classify = Classify::find()
            ->andWhere(['status' => Classify::STATUS_ACTIVE])
            ->select('id,title,parent_id')
            ->orderBy('parent_id ASC,id ASC')
            ->asArray()
            ->all();

        $result = [];
        foreach ($classify as $val) {
            if (!$val['parent_id']) {
                $result[$val['id']] = $val;
                $result[$val['id']]['sub'] = [];
            } else {
                array_push($result[$val['parent_id']]['sub'], $val);
            }
        }
        $count = Course::find()->andWhere(['status' => Course::STATUS_ACTIVE])->select('id')->count();
        return [
            'data' =>  array_values($result),
            'num' => $count
        ];
    }


}
